﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MonoTouchAutoBind
{
    class BindProtocol : MonotouchBindContainer
    {
        private string patternStart = @"@protocol\s+(?'className'\w+)(\s*<\s*(?'interface'[\w\s,]+)\s*>\s*)?";
        private System.Text.RegularExpressions.Regex regex = null;

        private string className, parentClassName;

        public override List<MonotouchBind> bindEngine
        {
            get
            {
                List<MonotouchBind> engines = new List<MonotouchBind>() 
                { 
                    new BindProperty(),
                    new BindMethod(),
                };
                return engines;
            }
        }


        public BindProtocol()
            : base()
        {
            regex = new System.Text.RegularExpressions.Regex(patternStart);
        }

        public override bool CanParse(string code)
        {
            return regex.IsMatch(code);
        }

        public override int ParseSelfStart(string code)
        {
            System.Text.RegularExpressions.Match match = regex.Match(code);
            className = match.Groups["className"].Value;
            parentClassName = match.Groups["interface"].Value;

            return match.Length;

        }

        public override int ParseSelfEnd(string code)
        {
            return code.Length;
        }

        public override string GenerateCSharpStart()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("[Model, BaseType(typeof({0}))]", this.parentClassName);
            sb.AppendLine();
            sb.AppendFormat("interface {0} {{", this.className);
            sb.AppendLine();

            return sb.ToString();
        }

        public override string GenerateCSharpEnd()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("}");
            sb.AppendLine();
            return sb.ToString();
        }

    }
}
